const crypto = require('crypto')
const ed25519 = require('ed25519')
const http = require('http')
const argv = process.argv
console.log(argv)
const secret = argv[2]
var hash = crypto.createHash('sha256').update(secret, 'utf8').digest();
var keypair = ed25519.MakeKeypair(hash);	
console.log('#')
console.log(keypair.privateKey.toString('hex'))
console.log('#')
console.log(keypair.publicKey.toString('hex'))
console.log('#')
function transfer(sendPublic,secret,recipient,amount,callback){
    var content = JSON.stringify({secret: secret, publicKey: sendPublic, amount: ~~amount, recipientPublicKey: recipient});
    // console.info(content)
    var options = {
        hostname: 'www.dlbchain.natapp1.cc',		//'127.0.0.1',
        method:'POST',
        path:'/api/transactions/',
        // port:3091,
        headers:{
            'Content-Type':'application/json',
            // 'Content-Type':'application/x-www-form-urlencoded',
            'Content-Length':content.length
        }
    }
    console.log(content)
    var request = http.request(options,function(response){
        // console.log('STATUS:'+response.statusCode);
        // console.log('HEADERS:'+JSON.stringify(response.headers));
        response.setEncoding('utf8');
        var result = ''
        response.on('data',(data)=>{
            result = result + data;
        })
        response.on('end', (data) => {
            // console.info(`body:${result}`)
            try{
                var str = result.toString()
                var obj = JSON.parse(str)
            } catch(err){
                return callback({code:0,msg:err})
            }
            // console.log(obj)
            return callback(obj)
        })
    })
    request.write(content);                
    request.end();
}
// console.log(keypair.publicKey.toString('hex'))
// transfer(keypair.publicKey.toString('hex'),secret,argv[2],argv[3],function(resp){
//     console.log(resp)
// })